package com.tube.fiddles.util;

import java.io.IOException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import org.apache.commons.codec.binary.Base64;

public abstract class DESUtil {

    private final static String KEY = "bvs-lu";
    private final static String TRANSFORMATION = "DES/ECB/PKCS5Padding";
    private final static String ALGORITHM = "DES";

    private static Key getKey() {
        Key key = null;
        try {
            KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
            generator.init(new SecureRandom(KEY.getBytes()));
            key = generator.generateKey();
            generator = null;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return key;
    }

    public static byte[] encrypt(byte[] byts) {
        byte[] byteFina = null;
        Cipher cipher;
        try {
            cipher = Cipher.getInstance(TRANSFORMATION);
            cipher.init(Cipher.ENCRYPT_MODE, getKey());
            byteFina = cipher.doFinal(byts);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }
        return byteFina;
    }

    public static String encrypt(String strMing) {
        byte[] byteMi = null;
        byte[] byteMing = null;
        String strMi = "";

        Base64 encoder = new Base64();
        try {
            byteMing = strMing.getBytes("utf-8");
            byteMi = encrypt(byteMing);
            strMi = encoder.encodeAsString(byteMi);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            encoder = null;
            byteMi = null;
            byteMing = null;
        }
        return strMi;
    }

    public static byte[] decrypt(byte[] bytd) {
        byte[] byteFina = null;
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance(TRANSFORMATION);
            cipher.init(Cipher.DECRYPT_MODE, getKey());
            byteFina = cipher.doFinal(bytd);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }
        return byteFina;
    }

    public static String decrypt(String strMi) {
        Base64 base64Decoder = new Base64();
        byte[] byteMing = null;
        byte[] byteMi = null;
        String strMing = "";
        try {
            byteMi = base64Decoder.decode(strMi);
            byteMing = decrypt(byteMi);
            strMing = new String(byteMing, "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            base64Decoder = null;
            byteMing = null;
            byteMi = null;
        }
        return strMing;
    }
    
    public static void main(String[] args) {
        String test = "root";
        
        String encrypttStr = encrypt(test);
        System.out.println(encrypttStr);
        
        System.out.println(decrypt(encrypttStr));
        
        String psw = "admin1234";
        
        String encryptpsw = encrypt(psw);
        System.out.println(encryptpsw);
        
        System.out.println(decrypt(encryptpsw));
    }
    
}
